package com.bird.system.application.menu

import cn.dev33.satoken.stp.StpUtil
import com.bird.system.application.menu.pl.ListMenuResponse
import com.bird.system.application.permission.RoleMenuRepository
import com.bird.system.domain.menu.MenuEntity
import org.springframework.stereotype.Service

@Service
class MenuQueryService(
    private val roleMenuRepository: RoleMenuRepository
) {

    /**
     * 查询可用菜单列表
     */
    fun listMenu(): List<ListMenuResponse> {
        val roleList = StpUtil.getRoleList()
        val menuEntities = roleMenuRepository.getMenuListByRoleId(roleList.map { it.toLong() })
        //下载完了之后
        return generateMenuTree(menuEntities, 0L)
    }

    /**
     * 生成菜单树
     */
    private fun generateMenuTree(menuEntities: List<MenuEntity>, parentId: Long): List<ListMenuResponse> {
        val menuTree = mutableListOf<ListMenuResponse>()
        val currentMenus = menuEntities.filter { it.parentId == parentId }
        currentMenus.forEach {
            val menu = it.toListMenuResponse()
            menu.children = generateMenuTree(menuEntities, it.id!!)
            menuTree.add(menu)
        }
        return menuTree
    }
}

infix fun String.likes(name: String): ((String) -> Unit) -> Unit {

    println("$this likes $name")
    return { it(name) }
}

fun main() {
    ("Alice" likes "Bob"){
        println("$it is very happy")
    }

}